/******************************************************************************
Input(s): Manufacturing_1977_2001.dta

This shell file specifies values/variable for
	- sigma
	- alphaL
	- alphaK
and calls files that
	1) Construct measures of productivity and distortions
	2) Trim outliers
	3) Output intermediate datasets containing the measures of productivity
		and distortions, as well as flags for outliers

******************************************************************************/

** ======================================================================
** THIS IS THE SHELL FILE TO CALL THE DIFFERENT FILES TO 
** CALCULATE MISALLOCATION AND COUNTERFACTUALS
** ======================================================================

clear all
set more off
local data "/FILE PATH GOES HERE/"
local code "/FILE PATH GOES HERE/"
local output "/FILE PATH GOES HERE/"

/*SECTION 1: Trimming*/
local S1 = 0
/*SECTION 2: Misallocation*/
local S2 = 0
/*SECTION 3: Trimming GO Version*/
local S3 = 0
/*SECTION 4: Misallocation GO Version*/
local S4 = 0

/*Trimming*/
if `S1' == 1 {

local num = 1 //specifies whether estimates in use have only the capital elasticity estimated using control-function approach (num=1) or whether both capital and labor elasticities have been estimated (num=2)
local types "5y" //specifies whether the parameters have been estimated using 5-year windows (5y) or 10-year windows (10y)
	foreach type of local types {
	/*VRTS with sigma estimated*/
	global sigma "sigma_`num'acf_vrts_`type'"
	global alphaL "aL_`num'acf_vrts_`type'"
	global alphaK "aK_`num'acf_vrts_`type'"
	global fname "vrts_`num'acf_sest_`type'"
	
	do "`code'6b_Misallocation_variables_and_trimming.do"
	}

}

/*********************************************
Baseline: ACF1, 5y, ASM, CM, Four Time Series
+ Robustness: CMF
+ Robustness: VM
+ Robustness: Fixed Profit Shares, CRTS
+ Robustness: ACF2
**********************************************/
if `S2' == 1 {
local trim "own" //options: own or none, specifies whether the sample has been trimmed for outliers (own=yes)
local type "5y" //specifies whether the parameters have been estimated using 5-year windows (5y) or 10-year windows (10y)

foreach num of numlist 1/1 {
	/*VRTS with sigma estimated*/
	global sigma "sigma_`num'acf_vrts_`type'"
	global alphaL "aL_`num'acf_vrts_`type'"
	global alphaK "aK_`num'acf_vrts_`type'"
	global fname "vrts_`num'acf_sest_`type'"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	
	/*CRTS with sigma estimated*/
	global sigma "sigma_`num'acf_vrts_`type'"
	global alphaL "aL_`num'acf_crts_`type'"
	global alphaK "aK_`num'acf_crts_`type'"
	global fname "crts_`num'acf_sest_`type'"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	
	/*VRTS with sigma = 3*/
	global sigma "sigma_3"
	global alphaL "aL_`num'acf_vrts_`type'"
	global alphaK "aK_`num'acf_vrts_`type'"
	global fname "vrts_`num'acf_s3_`type'"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	
	/*CRTS with sigma = 3*/
	global sigma "sigma_3"
	global alphaL "aL_`num'acf_crts_`type'"
	global alphaK "aK_`num'acf_crts_`type'"
	global fname "crts_`num'acf_s3_`type'"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	
	/*CRTS with sigma implied*/
	global sigma "sigma_`num'acf_crts_`type'"
	global alphaL "aL_`num'acf_crts_`type'"
	global alphaK "aK_`num'acf_crts_`type'"
	global fname "crts_`num'acf_simp_`type'"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	
	}
/*ACF instead of LP*/
foreach num of numlist 2/2 {
	/*VRTS ACF with sigma estimated*/
	global sigma "sigma_`num'acf_vrts_`type'"
	global alphaL "aL_`num'acf_vrts_`type'"
	global alphaK "aK_`num'acf_vrts_`type'"
	global fname "vrts_`num'acf_sest_`type'"
	global pftype "`type'_2"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	
	/*CRTS ACF with sigma = 3*/
	global sigma "sigma_3"
	global alphaL "aL_`num'acf_crts_`type'"
	global alphaK "aK_`num'acf_crts_`type'"
	global fname "crts_`num'acf_s3_`type'"
	global pftype "`type'_2"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	}

/*********************************************************
Baseline Originally : ACF1, ASM, 5y, CM, Four Time Series
Now Robustness: 10y, base
**********************************************************/
local types "base 10y"
local num = 1	
foreach type of local types {

	/*VRTS with sigma estimated*/
	global sigma "sigma_`num'acf_vrts_`type'"
	global alphaL "aL_`num'acf_vrts_`type'"
	global alphaK "aK_`num'acf_vrts_`type'"
	global fname "vrts_`num'acf_sest_`type'"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	
	/*CRTS with sigma = 3*/
	global sigma "sigma_3"
	global alphaL "aL_`num'acf_crts_`type'"
	global alphaK "aK_`num'acf_crts_`type'"
	global fname "crts_`num'acf_s3_`type'"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual.do"
	}

/*********************************************************
Baseline Originally : ACF1, ASM, 5y, CM, Four Time Series
Now Robustness: 5y_fk4
**********************************************************/
local types "5y_fk4"
local num = 1	
foreach type of local types {

	/*VRTS with sigma estimated*/
	global sigma "sigma_`num'acf_vrts_`type'"
	global alphaL "aL_`num'acf_vrts_`type'"
	global alphaK "aK_`num'acf_vrts_`type'"
	global fname "vrts_`num'acf_sest_5y_4"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual_fk4.do"
	
	/*CRTS with sigma = 3*/
	global sigma "sigma_3"
	global alphaL "aL_`num'acf_crts_`type'"
	global alphaK "aK_`num'acf_crts_`type'"
	global fname "crts_`num'acf_s3_5y_4"
	global pftype "`type'"
	global dtrim "`trim'"
	
	do "`code'2_c_Misallocation_Counterfactual_fk4.do"
	}

}

/*Trimming: Gross Output Version*/
if `S3' == 1 {

/*Types:
	lp: estimated parameters
	va: implied parameters from va estimates
	*/
local types "lp va crts"

foreach type of local types {
/*GO Estimated Parameters*/
	global sigma "sigma_`type'_go"
	global alphaL "aL_`type'_vrts_go"
	global alphaK "aK_`type'_vrts_go"
	global alphaM "aM_`type'_vrts_go"
	global fname "vrts_`type'_sest_go"
	
	do "`code'2_b_Misallocation_Variables_and_Trimming_go.do"
	}
}

/*Baeline: Gross Output Version*/
if `S4' == 1 {

/*Types:
	lp: estimated parameters
	va: implied parameters from va estimates
	*/
local types "lp va crts"

foreach type of local types {

	/*VRTS ACF with sigma estimated*/
	global sigma "sigma_`type'_go"
	global alphaL "aL_`type'_vrts_go"
	global alphaK "aK_`type'_vrts_go"
	global alphaM "aM_`type'_vrts_go"
	global fname "vrts_`type'_sest_go"
	global pftype "`type'"
	global dtrim "own"
	
	do "`code'2_c_Misallocation_Counterfactual_go.do"
	
	/*CRTS ACF with sigma = 3*/
	global sigma "sigma_3"
	global alphaL "aL_`type'_crts_go"
	global alphaK "aK_`type'_crts_go"
	global alphaM "aM_`type'_crts_go"
	global fname "crts_`type'_s3_go"
	global pftype "`type'"
	global dtrim "own"
	
	do "`code'2_c_Misallocation_Counterfactual_go.do"
	
	/*CRTS ACF with sigma = 4*/
	global sigma "sigma_4"
	global alphaL "aL_`type'_crts_go"
	global alphaK "aK_`type'_crts_go"
	global alphaM "aM_`type'_crts_go"
	global fname "crts_`type'_s4_go"
	global pftype "`type'"
	global dtrim "own"
	
	do "`code'2_c_Misallocation_Counterfactual_go.do"
	
	}
}


/*End of File*/
